package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.LsFeedback;
import com.example.xpvideo.entity.LsReport;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.PhpFeedbackReq;
import com.example.xpvideo.entity.req.PhpReportReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.PhpFeedbackRes;
import com.example.xpvideo.entity.res.PhpReportRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LsFeedbackMapper;
import com.example.xpvideo.mapper.PhpFeedbackMapper;
import com.example.xpvideo.service.PhpFeedbackService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PhpFeedbackServiceImpl implements PhpFeedbackService {
    @Autowired
    private PhpFeedbackMapper phpFeedbackMapper;
    @Autowired
    private LsFeedbackMapper lsFeedbackMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<PhpFeedbackRes>>> list(PhpFeedbackReq phpFeedbackReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String phone = phpFeedbackReq.getPhone();
        Integer type = phpFeedbackReq.getType();
        String nickname = phpFeedbackReq.getNickname();

        PageHelper.startPage(page, size);
        List<PhpFeedbackRes> list = phpFeedbackMapper.feedBackList(Wrappers.<LsFeedback>query()
                .eq(!ObjectUtils.isEmpty(phone), "lsr.phone", phone)
                .eq(!ObjectUtils.isEmpty(type), "feedback_type", type)
                .like(!ObjectUtils.isEmpty(nickname),"nickname", nickname)
                .eq("is_del",1));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PhpFeedbackRes>> listPageResult = new PageResult<>(count,pages,
                pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> delete(UpdateReq updateReq) throws ErrorException {
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsFeedback lsFeedback = new LsFeedback();
        lsFeedback.setId(id);
        lsFeedback.setIsDel(stage);
        lsFeedback.setUpdateTime(timeService.getTime());

        int res = lsFeedbackMapper.updateLsFeedback(lsFeedback);

        if(res > 0){
            return ResponseResult.responseResult(0,"意见反馈删除成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20003);
    }


}
